﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using UnityEngine.UI;
public class SpriteManager : SystemModule
{
    #region 切割图片常量
    const int SUBIMGWIDTH = 128;
    const int SUBIMGHEIGHT = 128;
    const int BIAS = 6;
    #endregion


    Dictionary<string, Sprite> spriteDic = new Dictionary<string, Sprite>();
    Dictionary<string, Sprite> spriteDicBench = new Dictionary<string, Sprite>();
    Dictionary<string, Vector2Int> itemDic = new Dictionary<string, Vector2Int>
    {
        { "Core", new Vector2Int(0,0) },
        { "Joint", new Vector2Int(0,0) },
        {"Helicopter",new Vector2Int(0,0) },
                { "RaySword", new Vector2Int(0,0) },
        { "Engine", new Vector2Int(0,0) },
        { "SteelFork", new Vector2Int(1,0)},
        { "SteelCorner", new Vector2Int(0,0)},
        { "SteelCross", new Vector2Int(1,1) },
        { "SteelLine", new Vector2Int(1,0) } 
    };

    public override void Initialize(Main m)
    {
        base.Initialize(m);
        //InitDic();
        InitItemSprite(itemDic);
        
    }
    private void InitDic()
    {
        var prefabs = ResourceManager.Instance.LoadAll<GameObject>("Prefab/Item");
        foreach(var e in prefabs)
        {
            SCollider collider = EntityUtilities.GetPrefab(EntityUtilities.GetIdByName(e.name)).GetComponent<SCollider>().Entity.GetComponent<SCollider>();
            if(collider == null)
            {
                Debug.LogError(e.name + " 没有Scollider组件");
            }
            if(collider.LocalMeshPos.Count == 0)
            {
                Debug.LogError(e.name + " MeshPosSize = 0");
            }
            Vector2Int minMaxX = new Vector2Int(collider.LocalMeshPos[0].x, collider.LocalMeshPos[0].x);
            Vector2Int minMaxY = new Vector2Int(collider.LocalMeshPos[0].y, collider.LocalMeshPos[0].y);
            foreach(var c in collider.LocalMeshPos)
            {
                if (c.x < minMaxX.x)
                    minMaxX.x = c.x;
                if (c.x > minMaxX.y)
                    minMaxX.y = c.x;

                if (c.y < minMaxY.x)
                    minMaxY.x = c.y; 
                if (c.y < minMaxY.y)
                    minMaxY.y = c.y;
            }

            Vector2Int center = new Vector2Int(-minMaxX.x, -minMaxY.x);
            itemDic.Add(e.name, center);
        }
    }
    private void InitItemSprite(Dictionary<string, Vector2Int> itemDic)
    {
        foreach(var it in itemDic)
        {
            var tmp = Resources.Load<Texture2D>("Sprite/EntitySprite/" + it.Key);
            if(tmp == null)
            {
                Debug.LogWarning("Sprite不存在" + it.Key);
                continue;
            }
            var xL = tmp.width / SUBIMGWIDTH;
            var yL = tmp.height / SUBIMGHEIGHT;
            for (int i=0;i<xL;i++)
            {
                for(int j=0;j<yL;j++)
                {
                    Texture2D subij = new Texture2D(SUBIMGWIDTH,SUBIMGHEIGHT);
                    //subij = new Texture2D(tmp.width, tmp.height);
                    for(int m = i * SUBIMGWIDTH; m<(i+1) * SUBIMGWIDTH; m++)
                    {
                        for(int n = j * SUBIMGHEIGHT; n < (j+1) * SUBIMGHEIGHT; n++)
                        {
                            subij.SetPixel(m - i * SUBIMGWIDTH, n - j * SUBIMGHEIGHT, 
                                tmp.GetPixel(m,n));
                        }
                    }
                    subij.Apply();
                    string name = it.Key + (i + BIAS - it.Value.x).ToString() + (j + BIAS - it.Value.y).ToString();
                    /*for(int m = 0; m<tmp.width; m++)
                    {
                        for(int n = 0; n < tmp.height; n++)
                        {
                            subij.SetPixel(m, n, tmp.GetPixel(m, n));  
                        }
                    }*/
                    Sprite tmpS = Sprite.Create(subij,new Rect(0,0,128,128), new Vector2(0.5f, 0.5f));
                    //Sprite tmpS = Sprite.Create(subij, new Rect(0, 0, tmp.width, tmp.height), new Vector2(0.5f, 0.5f));
                    spriteDicBench.Add(name, tmpS);
                }
            }
        }
    }
    public Sprite GetSprite(string name)
    {
        if (spriteDic.ContainsKey(name)) return spriteDic[name];
        return null;
    }
    public Sprite GetSpriteBench(string name)
    {
        if (spriteDicBench.ContainsKey(name)) 
            return spriteDicBench[name];
        return null;
    }
    public override void Enable()
    {
        base.Enable();
        spriteDic = new Dictionary<string, Sprite>();
        var list = ResourceManager.Instance.LoadAll<Sprite>("Sprite");
        foreach(var x in list)
        {
            spriteDic[x.name] = x;
        }
    }
}